<?php


namespace app\voip\func;


use app\voip\model\EPatriarch;
use app\voip\model\EProvince;
use function foo\func;

class ProvinceFunc
{
    /**
     * @param $data
     * @return array
     * @throws \think\exception\DbException
     */
    static function checkSave($data)
    {

        $resultData=[];
        foreach ($data as $key=>$val)
        {
            $resultData[$key]=[self::checkSavemy($key,0,0)];
            unset($val[0]);
            foreach ($val as $key1=>$val1){
                $resultData[$key][$key1]=[self::checkSavemy($key1,1,$resultData[$key][0])];
                unset($val1[0]);
                foreach ($val1 as $key2=>$val2){
                    $resultData[$key][$key1][$key2]=self::checkSavemy($val2,2,$resultData[$key][$key1][0]);
                }
            }

        }
        return $resultData;
    }

    /**
     * @param $key
     * @param $type
     * @param int $top_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    static private function checkSavemy($key,$type,$top_id=0)
    {
        $result=EProvince::get(['toponymy'=>$key,'top_id'=>$top_id,'type'=>$type]);
        if ($result){
            return $result->id;
        }else{
            $result=EProvince::create(['toponymy'=>$key,'type'=>$type,'top_id'=>$top_id]);
            return $result->id;
        }
    }

    /**
     * @param $where
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function getInfo($where)
    {
        $obj = new EProvince();
        return $obj->where($where)->select();
    }

    /**
     * @param $where
     * @return mixed
     */
    static public function find($where)
    {
        return EProvince::get($where);
    }
}